/*
 * Copyright 2014 by État de Genève (Switzerland)
 * All rights reserved.
 *
 * Author: ELCA Informatique SA
 */
define(['moment'], function(moment) {

    return {
        // Comparison equals of two date using moment js
        dateEq: dateEquals,
        // Comparison greater of two date using moment js
        dateGt: dateGreaterThan,
        // Comparison less of two date using moment js
        dateLt: dateLessThan,
        // Comparison greater or equals of two date using moment js
        dateGe: dateGreaterOrEquals,
        // Comparison less or equals of two date using moment js
        dateLe: dateLessOrEquals,
        // Return current date in moment js object
        currentMomentDate: currentMomentDate,
        // Reuturn day after add day
        addDay: addDay,
        // Reuturn day after subtract day
        subtractDay: subtractDay
    };

    /*
     * Function to compare two date that if one date is equal than other
     * @param date1 (Moment|String|Number|Date|Array)
     * @param date2 (Moment|String|Number|Date|Array)
     * @param truncate ('year', 'month', 'week', 'day', 'hour', 'minute', 'second')
     * @return true if equals, otherwise false
     */
    function dateEquals(date1, date2, truncate) {
        return moment(date1).isSame(moment(date2), truncate);
    }

    /*
     * Function to compare two date that if one date is greater than other
     * @param date1 (Moment|String|Number|Date|Array)
     * @param date2 (Moment|String|Number|Date|Array)
     * @param truncate ('year', 'month', 'week', 'day', 'hour', 'minute', 'second')
     * @return true if equals, otherwise false
     */
    function dateGreaterThan(date1, date2, truncate) {
        return moment(date1).isAfter(moment(date2), truncate);
    }

    /*
     * Function to compare two date that if one date is less than other
     * @param date1 (Moment|String|Number|Date|Array)
     * @param date2 (Moment|String|Number|Date|Array)
     * @param truncate ('year', 'month', 'week', 'day', 'hour', 'minute', 'second')
     * @return true if equals, otherwise false
     */
    function dateLessThan(date1, date2, truncate) {
        return dateGreaterThan(date2, date1, truncate);
    }

    /*
     * Function to compare two date that if one date is greater than or equals other
     * @param date1 (Moment|String|Number|Date|Array)
     * @param date2 (Moment|String|Number|Date|Array)
     * @param truncate ('year', 'month', 'week', 'day', 'hour', 'minute', 'second')
     * @return true if equals, otherwise false
     */
    function dateGreaterOrEquals(date1, date2, truncate) {
        return dateGreaterThan(date1, date2, truncate) || dateEquals(date1, date2, truncate);
    }

    /*
     * Function to compare two date that if one date is less than or equals other
     * @param date1 (Moment|String|Number|Date|Array)
     * @param date2 (Moment|String|Number|Date|Array)
     * @param truncate ('year', 'month', 'week', 'day', 'hour', 'minute', 'second')
     * @return true if equals, otherwise false
     */
    function dateLessOrEquals(date1, date2, truncate) {
        return dateLessThan(date1, date2, truncate) || dateEquals(date1, date2, truncate);
    }

    /*
     * Function to get the current date by moment js
     */
    function currentMomentDate() {
        return moment.utc().valueOf();
    }
    
    /*
     * Function get day after add
     * @param date
     * @return date after add
     */
    function addDay(date, n) {
        return moment.utc(date).add('days', n).valueOf();
    }
    
    /*
     * Function get day after subtract
     * @param date
     * @return date after subtract
     */
    function subtractDay(date, n) {
        return moment.utc(date).subtract('days', n).valueOf();
    }
});